package org.hsieh.tr.explore.ui;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.List;

import org.hsieh.tr.explore.annotation.TreeNodeChildren;
import org.hsieh.tr.explore.db.ISqlCommand;

/**
 * Not Thread-safe
 * 
 *@author hsieh 234aini@gmail.com
 * 
 */
public class SQLCommandManager {

    private PropertyChangeSupport support = new PropertyChangeSupport(this);

    private SQLCommandManager() {
    }

    private static SQLCommandManager instance = null;

    public static final SQLCommandManager getInstance() {
	if (instance == null) {
	    instance = new SQLCommandManager();
	}
	return instance;
    }

    @TreeNodeChildren
    private List<ISqlCommand> commands = new ArrayList<ISqlCommand>();

    public List<ISqlCommand> getCommands() {
	return commands;
    }

    public void addCommands(ISqlCommand command) {
	if (command != null)
	    commands.add(command);
	support.firePropertyChange("Command Change", null, command);
    }

    public void removeCommands(ISqlCommand command) {
	if (commands.remove(command))
	    support.firePropertyChange("Command Change", null, command);
    }

    public void clearCommands() {
	commands.clear();
	support.firePropertyChange("Command Change", null, null);
    }

    public void addPropertiesListener(PropertyChangeListener listener) {
	support.addPropertyChangeListener(listener);
    }

    public void removePropertiesListener(PropertyChangeListener listener) {
	support.removePropertyChangeListener(listener);
    }

}
